import { MARK } from '../define/variable'
import AudioManager from './audioManager'
import { Msg } from './msg'
import resManager from './resManager'
import { Tips } from './tips'
import { util } from './util'
/**
 * @description: 获取当前Canvas
 * @return {*}
 */
export const getCanvas = function (): cc.Node {
  const ret = cc.find('Canvas')
  return ret
}
/**
 * @description:提示
 * @param {*} text 内容
 * @param {*} timeValue 时间
 * @return {*}
 */
export const tips = async function (text, timeValue = 3, isOpactiy?: boolean): Promise<boolean> {
  return new Promise(async (resolve) => {
    try {
      const ret = await resManager.loadRes('/public/tips')
      if (ret) {
        const pre = cc.instantiate(ret)
        const tips = pre.getComponent('tips') as Tips
        tips.tipTxt = text
        tips.timeValue = timeValue
        tips.isOpactiy = isOpactiy == undefined ? false : true
        pre.on(MARK.NODE_EVENT, (res) => {
          resolve(res)
        })
        getCanvas().addChild(pre)
      }
    } catch (error) {
      console.log(error)
      resolve(false)
    }
  })
}
/**
 * @description:倒计时提示
 * @param {*} text 内容
 * @param {*} timeValue 时间
 * @return {*}
 */
export const tipsTime = async function (text, timeValue = 5, isOpactiy?: boolean): Promise<boolean> {
  return new Promise(async (resolve) => {
    try {
      const ret = await resManager.loadRes('/public/tips')
      if (ret) {
        const pre = cc.instantiate(ret)
        const tips = pre.getComponent('tips') as Tips
        tips.tipTxt = text
        tips.timeValue = timeValue
        tips.canShowTime = true
        tips.isOpactiy = isOpactiy == undefined ? false : true
        pre.on(MARK.NODE_EVENT, (res) => {
          resolve(res)
        })
        getCanvas().addChild(pre)
      }
    } catch (error) {
      console.log(error)
      resolve(false)
    }
  })
}
/**
 * @description:信息提示
 * @param {*} text 内容
 * @return {*}
 */
export const msgSelect = async function (text): Promise<boolean> {
  return new Promise(async (resolve) => {
    try {
      playEffect('btn')
      const ret = await resManager.loadRes('/public/msg')
      if (ret) {
        const pre = cc.instantiate(ret)
        const msg = pre.getComponent('msg') as Msg
        msg.tipTxt = text
        msg.isSingle = false
        pre.on(MARK.NODE_EVENT, (res) => {
          resolve(res)
        })
        getCanvas().addChild(pre)
      }
    } catch (error) {
      console.log(error)
      resolve(false)
    }
  })
}
/**
 * @description:公告提示
 * @param {*} text 内容
 * @return {*}
 */
export const noticeMsg = async function (text): Promise<boolean> {
  return new Promise(async (resolve) => {
    try {
      playEffect('btn')
      const ret = await resManager.loadRes('/public/notice')
      if (ret) {
        const pre = cc.instantiate(ret)
        const msg = pre.getComponent('msg') as Msg
        msg.tipTxt = text
        msg.isSingle = true
        pre.on(MARK.NODE_EVENT, (res) => {
          resolve(res)
        })
        getCanvas().addChild(pre)
      }
    } catch (error) {
      console.log(error)
      resolve(false)
    }
  })
}
/**
 * @description:倒计时提示
 * @param {*} text 内容
 * @return {*}
 */
export const msgCheck = async function (text): Promise<boolean> {
  return new Promise(async (resolve) => {
    try {
      playEffect('btn')
      const ret = await resManager.loadRes('/public/msg')
      if (ret) {
        const pre = cc.instantiate(ret)
        const msg = pre.getComponent('msg') as Msg
        msg.tipTxt = text
        msg.isSingle = true
        pre.on(MARK.NODE_EVENT, (res) => {
          resolve(res)
        })
        getCanvas().addChild(pre)
      }
    } catch (error) {
      console.log(error)
      resolve(false)
    }
  })
}

export const playLoopMusic = async function (): Promise<boolean> {
  return new Promise(async (resolve) => {
    const index = util.getRandomInt(1, 4)
    resManager.loadRes<cc.AudioClip>(`/music/bgmusic${index}`, cc.AudioClip).then((res) => {
      if (res) {
        const audioId = AudioManager.playMusic(res)
        if (audioId) {
          cc.audioEngine.setFinishCallback(audioId, () => {
            return this.playLoopMusic()
          })
        } else {
          resolve(false)
        }
      }
    })
  })
}
export const playEffect = async function (effectName: string, path?: string): Promise<boolean> {
  let dir = path ? `${path}/${effectName}` : `/effect/${effectName}`
  return new Promise(async (resolve) => {
    resManager.loadRes<cc.AudioClip>(dir, cc.AudioClip).then((res) => {
      if (res) {
        const audioId = AudioManager.playEffect(res)
        if (audioId) {
          cc.audioEngine.setFinishCallback(audioId, () => {
            return resolve(true)
          })
        } else {
          resolve(false)
        }
      } else {
        resolve(false)
      }
    })
  })
}
/**打开训练记录 */
export const trainOpen = async function (): Promise<boolean> {
  return new Promise(async (resolve) => {
    const ret = await resManager.loadRes('/hall/trainRecord')
    if (ret) {
      const pre = cc.instantiate(ret)
      const rootNode = cc.find('Canvas/modules')
      rootNode.addChild(pre)
      pre.on(MARK.NODE_EVENT, (res: boolean) => {
        resolve(res)
      })
      return
    }
    tipsTime('资源错误', 5)
  })
}
/**打开帮助 */
export const helpOpen = async function (gamePath?: IGamePath): Promise<boolean> {
  return new Promise(async (resolve) => {
    const ret = await resManager.loadRes('/hall/help')
    if (ret) {
      const pre = cc.instantiate(ret)
      if (gamePath) {
        pre.getComponent('help').gamePath = gamePath
      }
      const rootNode = getCanvas()
      rootNode.addChild(pre)
      pre.on(MARK.NODE_EVENT, (res: boolean) => {
        resolve(res)
      })
      return
    }
    tipsTime('资源错误', 5)
  })
}
/**打开设置 */
export const setingOpen = async function (): Promise<boolean> {
  return new Promise(async (resolve) => {
    const ret = await resManager.loadRes('/hall/seting')
    if (ret) {
      const pre = cc.instantiate(ret)
      const rootNode = cc.find('Canvas/modules')
      rootNode.addChild(pre)
      pre.on(MARK.NODE_EVENT, (res: boolean) => {
        resolve(res)
      })
      return
    }
    tipsTime('资源错误', 5)
  })
}

/**打开游戏得分*/
export const openScore = async function (score): Promise<boolean> {
  return new Promise(async (resolve) => {
    const ret = await resManager.loadRes('/public/score')
    if (ret) {
      const pre = cc.instantiate(ret)
      pre.getComponent('score').score = score
      getCanvas().addChild(pre)
      pre.on(MARK.NODE_EVENT, (res: boolean) => {
        resolve(res)
      })
      return
    }
    tipsTime('加载资源错误', 3)
  })
}
/**打开开始游戏统计界面*/
export const openGameStatis = async function (scoreData): Promise<boolean> {
  return new Promise(async (resolve) => {
    const ret = await resManager.loadRes('/public/gameStatis')
    if (ret) {
      const pre = cc.instantiate(ret)
      pre.getComponent('gameStatis').scoreData = scoreData
      getCanvas().addChild(pre)
      pre.on(MARK.NODE_EVENT, (res: boolean) => {
        resolve(res)
      })
      return
    }
    tipsTime('加载资源错误', 3)
  })
}
/**关闭退出系统*/
export const onExitSys = async function () {
  try {
    cc.game.end()
  } catch (error) {
    console.log(error)
  }
  return Promise.resolve(false)
}
